﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Register.Command;
using Register.DAL;
using Register.DBUtility;
using Register.IDAL;
using Register.Model;
using System.Data;
using System.Data.SqlClient;
namespace Register.DAL
{
    /// <summary> 
    /// FileName: SystemLoggerDAL.cs    
    /// CLRVersion: 4.0.30319.269    
    /// Author: DoubleZ    
    /// Corporation:     
    /// Description:     
    /// DateTime: 2012/7/13 19:34:14    
    /// </summary>
 public   class SystemManageDAL:ISystemManageDAL
 {
     private string tableName = "System_loggerInfo";
     #region 系统日志查询
    public List<Model.LogerInfo> GetPagedLogerInfo(int size, int pageIndex, string where) {
        return DataSetToList(DbHelperSQL.getPagedData(" * ", "Logger_Time", where, tableName, OrderBy.DESC, pageIndex, size));
     }
    public int GetLogerInfoCount(string where) {
        int count = 0;
        StringBuilder str = new StringBuilder();
        str.Append(" select count(*) from ");
        str.Append(tableName);
        if (where != null)
        {
            str.Append(" where ");
            str.Append(where);
            count = (int)DbHelperSQL.GetSingle(str.ToString());
        }
        else
        {
            count = (int)DbHelperSQL.GetSingle(str.ToString());
        }
        return count;
     
    
    }
    private List<Model.LogerInfo> DataSetToList(DataSet ds)
    {
        List<Model.LogerInfo> list = new List<Model.LogerInfo>();
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            LogerInfo sch = new LogerInfo();
            sch.Logger_ID = Convert.ToInt64(dr["Logger_ID"]);
            sch.Logger_Info = (string)dr["Logger_Info"];
            sch.Logger_Level = (string)dr["Logger_Level"];
            sch.Logger_Name = (string)dr["Logger_Name"];
            sch.Logger_State = Convert.ToBoolean(dr["Logger_State"]);
            sch.Logger_Thread = (string)dr["Logger_Thread"];
            sch.Logger_Time = (DateTime)dr["Logger_Time"];
            
            list.Add(sch);
        }
        return list;
    }
     #endregion

     #region 获取个人信息
   public  List<AdminInfo> GetAdminInfo(string AdminId) {
       String sql = " select  Administrator_Hosptial,Administrator_ID,Administrator_Name,Administrator_Password from System_adminInfo  where Administrator_ID='" + AdminId + "'";
       List<AdminInfo> list = new List<AdminInfo>();
            SqlDataReader reader = DbHelperSQL.ExecuteReader(sql);
            list = ChangeReaderToAdminInfoList(reader);
            reader.Close();
            return list;
      
      }
   private List<AdminInfo> ChangeReaderToAdminInfoList(SqlDataReader reader)
        {
            List<AdminInfo> list = new List<AdminInfo>();
            while (reader.Read())
            {
                AdminInfo paramter = new AdminInfo();
              
                paramter.Administrator_Hosptial = reader.GetString(0);
                paramter.Administrator_ID = reader.GetString(1);
                paramter.Administrator_Name = reader.GetString(2);
                paramter.Administrator_Password = reader.GetString(3);
                
                list.Add(paramter);
            }
            return list;
        }
   
     #endregion
     #region 更新个人信息
  public int UpdateAdminInfo(AdminInfo schInfo) {
      StringBuilder strSql = new StringBuilder();
      strSql.Append("update System_adminInfo set ");
      strSql.Append("Administrator_Name=@Administrator_Name,");
      strSql.Append("Administrator_Password=@Administrator_Password,");
      strSql.Append("Administrator_LastRegTime=@Administrator_LastRegTime  ");

      strSql.Append(" where Administrator_ID=@Administrator_ID");
      SqlParameter[] parameters = {	
					new SqlParameter("@Administrator_Name", SqlDbType.VarChar,20),
					new SqlParameter("@Administrator_Password", SqlDbType.Char,32),
					new SqlParameter("@Administrator_LastRegTime", SqlDbType.DateTime),
					new SqlParameter("@Administrator_ID",SqlDbType.Char,14)
                                       };

      parameters[0].Value = schInfo.Administrator_Name;

      parameters[1].Value = schInfo.Administrator_Password;
      parameters[2].Value = schInfo.Administrator_LastRegTime;
      parameters[3].Value = schInfo.Administrator_ID;

      return DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
   }
     #endregion
     #region 验证个人用户名
  public bool Exists(string adminName) { 
     StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from System_adminInfo");
            strSql.Append(" where Administrator_Name=@Administrator_Name ");
            SqlParameter[] parameters = {
					new SqlParameter("@Administrator_Name", SqlDbType.VarChar,20)};
            parameters[0].Value = adminName;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

  
  
     #endregion
     #region 检验旧密码是否输入正确
     public bool ExistsPwd(string pwd, string adminID) { 
   StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from System_adminInfo");
            strSql.Append(" where Administrator_Password=@Administrator_Password and Administrator_ID=@Administrator_ID");
            SqlParameter[] parameters = {
					new SqlParameter("@Administrator_Password", SqlDbType.Char,32),
                    new SqlParameter("@Administrator_ID",SqlDbType.Char,14)
                                        
                                        };
            parameters[0].Value = pwd;
            parameters[1].Value=adminID;
            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }

     
     #endregion
 }
}
